import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue'
import home from '../components/home.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'login'
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/home',
    component:home
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  //  to:访问的路径
  // form:从哪里跳转而来
  // next: 放行
  if (to.path==='/login') {
    return next()
  }
  const token=window.sessionStorage.getItem('token')
  if (!token) {
    return next('/login')
  }
  next()
})
export default router
